package spring_root.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import spring_root.po.User;

/**
 * 登录检查拦截器
 * 
 * @author liujianjun
 *
 */

public class loginInterceptor implements HandlerInterceptor {
	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		User u = (User) request.getSession().getAttribute("loginUser");
		System.out.println(u);
		String contextpath = request.getServletContext().getContextPath();
		System.out.println(contextpath);
		if (u != null) {
			return true;
		} else {
			System.out.println("借助");
			response.sendRedirect(contextpath + "/");
			return false;

		}
	}
}
